package com.test.daily.leetcode.y2022.m06.day0612.v01;

/**
 * @author Tom on 2022/6/12
 */
public class Solution {
    public static double livePossibilty(int col, int row, int k, int N, int M) {
        return process(col, row, k, N, M)/Math.pow(4, k);
    }

    public static int process(int col, int row, int rest, int M, int N) {
        if (col < 0 || row < 0 || col > M || row > N) {
            return -1;
        }
        if (rest == 0) {
            return 1;
        }
        int ways = 0;
        ways += process(col - 1, row, rest - 1, N, M);
        ways += process(col + 1, row, rest - 1, N, M);
        ways += process(col, row + 1, rest - 1, N, M);
        ways += process(col, row - 1, rest - 1, N, M);
        return ways;
    }

    public static int livePossibilty1(int col, int row, int k, int N, int M){
        int[][][] dp = new int[col][row][k+1];
        for(int i = 0; i < col; i++){
            for(int j = 0; j < row; j++){
                dp[i][j][0] = 1;
            }
        }
        for(int i = 0; i < col; i++){
            for(int j = 0; j < row; j++){
                for(int rest = 1; rest <= k; rest++ ){
                    dp[i][j][rest] = pick(dp, i,j, rest, N, M);
                }
            }
        }
        return dp[col][row][k];
    }

    public static int pick(int[][][] dp, int col, int row, int k, int N, int M){
        if(col < 0 || row < 0 || col > N - 1 || row > M -1){
            return 0;
        }
        return dp[k][row][col];
    }
}
